home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap18 / Emf12 / Emf12.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.5 KB  |  120 lines

  1. /*---------------------------------------
  2.    EMF12.C -- Enhanced Metafile Demo #12
  3.               (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. TCHAR szClass [] = TEXT ("EMF12") ;
  9. TCHAR szTitle [] = TEXT ("EMF12: Enhanced Metafile Demo #12") ;
  10.  
  11. void DrawRuler (HDC hdc, int cx, int cy)
  12. {
  13.      int     iAdj, i, iHeight ;
  14.      LOGFONT lf ;
  15.      TCHAR   ch ;
  16.  
  17.      iAdj = GetVersion () & 0x80000000 ? 0 : 1 ;
  18.  
  19.           // Black pen with 1-point width
  20.  
  21.      SelectObject (hdc, CreatePen (PS_SOLID, cx / 72 / 6, 0)) ;
  22.  
  23.           // Rectangle surrounding entire pen (with adjustment)
  24.  
  25.      Rectangle (hdc, iAdj, iAdj, cx + iAdj + 1, cy + iAdj + 1) ;
  26.  
  27.           // Tick marks
  28.           
  29.      for (i = 1 ; i < 96 ; i++)
  30.      {
  31.                if (i % 16 == 0) iHeight = cy /  2 ;    // inches
  32.           else if (i %  8 == 0) iHeight = cy /  3 ;    // half inches
  33.           else if (i %  4 == 0) iHeight = cy /  5 ;    // quarter inches
  34.           else if (i %  2 == 0) iHeight = cy /  8 ;    // eighths
  35.           else                  iHeight = cy / 12 ;    // sixteenths
  36.  
  37.           MoveToEx (hdc, i * cx / 96, cy, NULL) ;
  38.           LineTo   (hdc, i * cx / 96, cy - iHeight) ;
  39.      }
  40.           // Create logical font 
  41.  
  42.      FillMemory (&lf, sizeof (lf), 0) ;
  43.      lf.lfHeight = cy / 2 ;
  44.      lstrcpy (lf.lfFaceName, TEXT ("Times New Roman")) ;
  45.  
  46.      SelectObject (hdc, CreateFontIndirect (&lf)) ;
  47.      SetTextAlign (hdc, TA_BOTTOM | TA_CENTER) ;
  48.      SetBkMode    (hdc, TRANSPARENT) ;
  49.  
  50.           // Display numbers
  51.  
  52.      for (i = 1 ; i <= 5 ; i++)
  53.      {
  54.           ch = (TCHAR) (i + '0') ;
  55.           TextOut (hdc, i * cx / 6, cy / 2, &ch, 1) ;
  56.      }
  57.           // Clean up
  58.  
  59.      DeleteObject (SelectObject (hdc, GetStockObject (SYSTEM_FONT))) ;
  60.      DeleteObject (SelectObject (hdc, GetStockObject (BLACK_PEN))) ;
  61. }
  62.  
  63. void CreateRoutine (HWND hwnd)
  64. {
  65.      HDC          hdcEMF ;
  66.      HENHMETAFILE hemf ;
  67.      int          cxMms, cyMms, cxPix, cyPix, xDpi, yDpi ;
  68.      
  69.      hdcEMF = CreateEnhMetaFile (NULL, TEXT ("emf12.emf"), NULL,
  70.                                  TEXT ("EMF13\0EMF Demo #12\0")) ;
  71.      
  72.      cxMms = GetDeviceCaps (hdcEMF, HORZSIZE) ;
  73.      cyMms = GetDeviceCaps (hdcEMF, VERTSIZE) ;
  74.      cxPix = GetDeviceCaps (hdcEMF, HORZRES) ;
  75.      cyPix = GetDeviceCaps (hdcEMF, VERTRES) ;
  76.      
  77.      xDpi = cxPix * 254 / cxMms / 10 ;
  78.      yDpi = cyPix * 254 / cyMms / 10 ;
  79.      
  80.      DrawRuler (hdcEMF, 6 * xDpi, yDpi) ;
  81.      
  82.      hemf = CloseEnhMetaFile (hdcEMF) ;
  83.      
  84.      DeleteEnhMetaFile (hemf) ;
  85. }
  86.  
  87. void PaintRoutine (HWND hwnd, HDC hdc, int cxArea, int cyArea)
  88. {
  89.      ENHMETAHEADER emh ;
  90.      HENHMETAFILE  hemf ;
  91.      POINT         pt ;
  92.      int           cxImage, cyImage ;
  93.      RECT          rect ;
  94.      
  95.      SetMapMode (hdc, MM_HIMETRIC) ;
  96.      
  97.      SetViewportOrgEx (hdc, 0, cyArea, NULL) ;
  98.      
  99.      pt.x = cxArea ;
  100.      pt.y = 0 ;
  101.      
  102.      DPtoLP (hdc, &pt, 1) ;
  103.      
  104.      hemf = GetEnhMetaFile (TEXT ("emf12.emf")) ;
  105.      
  106.      GetEnhMetaFileHeader (hemf, sizeof (emh), &emh) ;
  107.      
  108.      cxImage = emh.rclFrame.right - emh.rclFrame.left ;
  109.      cyImage = emh.rclFrame.bottom - emh.rclFrame.top ;
  110.      
  111.      rect.left   = (pt.x - cxImage) / 2 ;
  112.      rect.top    = (pt.y + cyImage) / 2 ;
  113.      rect.right  = (pt.x + cxImage) / 2 ;
  114.      rect.bottom = (pt.y - cyImage) / 2 ;
  115.      
  116.      PlayEnhMetaFile (hdc, hemf, &rect) ;
  117.      
  118.      DeleteEnhMetaFile (hemf) ;
  119. }
  120.